package com.chen.controller.admin.post;

import com.chen.controller.admin.post.vo.*;
import com.chen.convert.post.PostConvert;
import com.chen.dal.dateobject.post.PostDO;
import com.chen.pojo.CommonResult;
import com.chen.pojo.PageResult;
import com.chen.service.post.PostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/admin/post")
@Slf4j
@Tag(name = "后台-岗位管理")
@Validated
public class PostController {
    @Autowired
    private PostService postService;

    /**
     * 创建岗位
     * @param reqVO
     * @return
     */
    @PostMapping("/create")
    @Operation(summary = "创建岗位")
    // todo 权限校验
    public CommonResult<Long> createPost(@RequestBody @Valid PostCreateReqVO reqVO){
        PostDO post = postService.createPost(reqVO);
        return CommonResult.success(post.getId());
    }

    /**
     * 更新岗位
     * @param reqVO
     * @return
     */
    @PutMapping("/update")
    @Operation(summary = "更新岗位")
    //todo 权限校验
    public CommonResult<Boolean> updatePost(@RequestBody @Valid PostUpdateReqVO reqVO){
        postService.updatePost(reqVO);
        return CommonResult.success(true);
    }

    /**
     * 删除岗位
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除岗位")
    //todo 权限校验
    public CommonResult<Boolean> deletePost(@RequestParam("id") Long id){
        postService.deletePost(id);
        return CommonResult.success(true);
    }

    /**
     * 更新岗位状态
     * @param id
     * @param status
     * @return
     */
    @PutMapping("/update/status")
    @Operation(summary = "更新状态")
    //todo 权限校验
    public CommonResult<Boolean> updateStatus(@RequestParam("id") Long id,@RequestParam("status")Integer status){
        postService.updateStatus(id,status);
        return CommonResult.success(true);
    }

    /**
     * 根据id获取岗位信息
     * @param id
     * @return
     */
    @GetMapping("/get")
    @Operation(summary = "根据id获取岗位信息")
    //todo 权限校验
    public CommonResult<PostRespVO> getPostById(@RequestParam("id") Long id){
        PostRespVO respVO= postService.getPostById(id);
        return CommonResult.success(respVO);
    }

    /**
     *      分页获取岗位信息
     * @param reqVO
     * @return
     */
    @GetMapping("/get/page")
    @Operation(summary = "分页获取岗位信息")
    //todo 权限校验
    public CommonResult<PageResult<PostRespVO>> getPostPage(@Validated PostGetPageReqVO reqVO){
        PageResult<PostDO> postPageResult= postService.getPostPage(reqVO);
        PageResult<PostRespVO> postPageResultRespVO = PostConvert.CONVERT.convert03(postPageResult);
        return CommonResult.success(postPageResultRespVO);
    }

    /**
     * 分页获取精简岗位信息
     * @param reqVO
     * @return
     */
    @GetMapping("/get/simple")
    @Operation(summary = "分页获取精简岗位信息")
    //todo 权限校验
    public CommonResult<List<PostSimpleRespVO>> getPostSimpleList(@Validated PostGetSimpleListReqVO reqVO){
        List<PostDO> postSimpleList= postService.getPostSimpleList(reqVO.getName());
        List<PostSimpleRespVO> simpleSimpleListRespVO =PostConvert.CONVERT.convert03(postSimpleList);
        return CommonResult.success(simpleSimpleListRespVO);
    }

}
